﻿using System;
using System.Collections.Generic;
using System.Linq; 
using System.Xml.Linq;
namespace ITXMOV
{
    public partial class ApplicationBase
    {
        public string OracleDB { get; set; }
        public string AgentID { get; set; }
        public string UserID { get; set; }
        public string TermNo { get; set; }
        public bool IsOnline { get; set; }
        public static readonly ApplicationBase Instance = new ApplicationBase();

        public IntexReptServBLClient WCFClient
        {
            get;
            set;
        }
        string dbuser = "intex";
        string intex = "intex1";
        string dbpwd = "intex";// "intexdbadmin";
        public ApplicationBase()
        {
            WCFClient = new IntexReptServBLClient(); 
            GetID();
            UserID = "SUNLT";
            TermNo = "01";
            IsOnline = true;
            OracleDB = "user id=" + dbuser + ";data source=" + intex + ";password=" + dbpwd + ";Unicode=True;" + "Persist Security Info=True;";        
        }
         
        public void GetID()
        {
            string command = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<QueryBuilder>" +
                "<AGENTID>" + AgentID + "</AGENTID>" +
                "<DBTYPE>ORACLE</DBTYPE>" +
                "<CommandType>RequestAgentStart</CommandType>" +
                "</QueryBuilder>";


            ImageResult result = WCFClient.GetInfo(command);
            XDocument xdoc = XDocument.Parse(result.str0);
            string field = "";
            string value = "";
            foreach (XElement tables in xdoc.Elements())
            {
                foreach (XElement rows in tables.Elements())
                {
                    foreach (XElement columns in rows.Elements())
                    {
                        field = columns.Name.ToString();
                        value = columns.Value.ToString();
                        if (field == "AGENTID")
                        {
                            AgentID = value;
                            IsOnline = true;
                        }
                    }
                }
            } 
        }
        public void QAGENT()
        {
            string command = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                  "<QueryBuilder>" +
                   "<AGENTID>" + AgentID + "</AGENTID>" +
                  "<QAGENT>ORACLE</QAGENT>" +
                  "<CommandType>RequestAgentStart</CommandType>" +
                  "</QueryBuilder>";
            WCFClient.GetInfo(command);
        }
        ~ApplicationBase()
        {
            if (WCFClient.State != System.ServiceModel.CommunicationState.Closed)
            {
                QAGENT();
            }
        }
    }
}
